#include "userwindow.h"
#include "ui_userwindow.h"

userwindow::userwindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::userwindow)
{
    ui->setupUi(this);
    //设置只读
    ui->lineEdit_7->setReadOnly(true);
    ui->lineEdit_8->setReadOnly(true);
    ui->lineEdit_9->setReadOnly(true);
    ui->lineEdit_10->setReadOnly(true);
    //向主界面发送flush信号
    connect(&editinfo1,editinfo::send_edit_flush,this,userwindow::emit_edit_flush);
    //直接处理printinfo
    connect(&editinfo1,editinfo::send_edit_flush2,this,userwindow::printinfo);
    connect(&bk,&book::send_show_book,this,userwindow::reshow);
    connect(&bk,book::send_edit_flush3,this,userwindow::printinfo);
    connect(ui->pushButton_13, &QPushButton::clicked, this, &userwindow::consuinfo);
    connect(ui->pushButton_4, &QPushButton::clicked, this, &userwindow::consuption);
    connect(ui->pushButton_8, &QPushButton::clicked, this, &userwindow::emit_user_balance);
    connect(ui->comboBox,SIGNAL(currentTextChanged(QString)),this,SLOT(SetFlag(QString)));

}

userwindow::~userwindow()
{
    delete ui;
}
//处理从个人信息编辑页面来的刷新信号
void userwindow::emit_edit_flush(){
    emit(send_user_flush());
}
//在个人信息页面显示个人信息
void userwindow::printinfo(QString user_id, QString name, QString flag, QString carid, QString phone, QString email, QString register_time,QString fare,QString username){
    ui->lineEdit->setText(name);//姓名
    ui->lineEdit_2->setText(phone);//电话
    ui->lineEdit_3->setText(email);//邮箱
    ui->lineEdit_4->setText(flag);//类型
    ui->lineEdit_5->setText(register_time);//注册时间
    ui->lineEdit_6->setText(fare);//余额
    QString flagname;

    //查询当前用户拥有的汽车
    //QString inquire_2 = QString("select carid as car,cartype as type from carinfo where username = '%1'").arg(username);
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView);
    user=username;
    query.exec(QString("select * from userinfo where username='%1'").arg(user));
    query.next();
    //用户id
    QString id=this->query.value("id").toString();
    this->user_id=id;
    mode->setTable("carinfo");
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);

    mode->setFilter(QObject::tr("username = '%1'").arg(user)); //查询当前用户
    mode->select();
    mode->removeColumn(0);//不显示第一列id
    mode->removeColumn(3);//不显示第3列username
    mode->setHeaderData(0,Qt::Horizontal,tr("car"));//设置表头车牌号 中文报错未解决
    mode->setHeaderData(1, Qt::Horizontal, tr("type"));//
    ui->tableView->setModel(mode);



    //设置预订界面car_id
    QStringList strings,strings1;
    QString inquire_2 = QString("select carid as car,cartype as type from carinfo where username = '%1'").arg(username);
    QSqlQuery query(inquire_2);
    while (query.next()) {
        QString carid = query.value(0).toString();
        strings.append(carid);
    };
    ui->comboBox_2->clear();
    ui->comboBox_2->addItems(strings);
    //设置默认时间
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    //设置车位
    QString str1 = QString("select flag from userinfo where username = '%1'").arg(user);
    query.exec(str1);

    while(query.next())
    {
        flagname = query.value(0).toString();
    }
    QString str2 = QString("select id from place where tag = '%1' and status = 0").arg(flagname);
    query.exec(str2);
    while(query.next())
    {
        QString place_id = query.value(0).toString();
        strings1.append(place_id);
    };
    ui->comboBox_4->clear();
    ui->comboBox_4->addItems(strings1);

    //设置停车预约号的选车
    QString str_book = QString("select car_id from book where user_id='%1' and status=0;").arg(user_id);
    QStringList strings3;
    QSqlQuery query1(str_book);
    while(query1.next())
    {
        QString car_id = query1.value(0).toString();

        QString str1 = QString("select carid from carinfo where id=%1;").arg(car_id);
        QSqlQuery query2(str1);
        QString carid;
        if(query2.first()){carid=query2.value(0).toString();}
        strings3.append(carid);
    };
    ui->comboBox_5->clear();
    ui->comboBox_5->addItems(strings3);

    //设置出车停好的车
    QString str_stop = QString("select car_id from stopinfo where user_id='%1' and outtime is NULL;").arg(user_id);
    QStringList strings4;
    QSqlQuery query_stop(str_stop);
    while(query_stop.next())
    {
        QString car_id = query_stop.value(0).toString();

        QString str1 = QString("select carid from carinfo where id=%1;").arg(car_id);
        QSqlQuery query_car(str1);
        QString carid2;
        if(query_car.first()){carid2=query_car.value(0).toString();}
        strings4.append(carid2);
    };

    ui->comboBox_6->clear();
    ui->comboBox_6->addItems(strings4);
}
//个人信息页面编辑个人信息按钮槽反应//弹出编辑页面
void userwindow::on_pushButton_6_clicked()
{
    editinfo1.setuser(this->user);
    editinfo1.show();
}

//汽车信息编辑
void userwindow::on_pushButton_10_clicked()
{
    //caredit car_edit;
    car_edit.setuser(user);//把当前用户传入 增加汽车绑定用户
    car_edit.setflag(flag);//设置为用户操作
    car_edit.setread();
    car_edit.show();
}
//刷新汽车再次查询
void userwindow::on_pushButton_11_clicked()
{
    QSqlTableModel*mode= new QSqlTableModel(ui->tableView);
    mode->setTable("carinfo");
    mode->setFilter(QObject::tr("username = '%1'").arg(user)); //查询当前用户
    mode->select();
    mode->removeColumn(0);//不显示第一列id
    mode->removeColumn(3);//不显示第3列username
    mode->setHeaderData(0,Qt::Horizontal,tr("car"));//设置表头车牌号 中文报错未解决
    mode->setHeaderData(1, Qt::Horizontal, tr("type"));//
    ui->tableView->setModel(mode);
    QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("刷新成功"), QMessageBox::Yes);
}

void userwindow::on_pushButton_5_clicked()
{
    this->close();
    emit(send_show_login());
}
//预约
void userwindow::on_pushButton_clicked()
{

    QString place_id = ui->comboBox_4->currentText();//位置号
    QString carid = ui->comboBox_2->currentText();//车号
    QString time = ui->dateTimeEdit->text();      //时间
    QString user_id,car_id;
    int userid1,carid1,placeid;
    QSqlQuery query;
    QString str1 = QString("select id from userinfo where username = '%1'").arg(user);
    query.exec(str1);
    while(query.next())
    {
        user_id = query.value(0).toString();
        userid1 = user_id.toInt();
        placeid = place_id.toInt();
    }
    QString str2 = QString("select id from carinfo where carid = '%1'").arg(carid);
    query.exec(str2);
    while(query.next())
    {
        car_id = query.value(0).toString();
        carid1 = car_id.toInt();
    }

    if(query.exec(QString("select count(*) from book where car_id='%1' and status=0").arg(car_id))){
        while (query.next()) {
            if(query.isActive()){
                if((query.value(0).toInt()>0))//判断是否已经预订
                {qDebug()<<query.value(0).toInt();
                    QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("此车已经预订了"), QMessageBox::Yes);

                }
                else{
                    QString str3 = QString("insert into book(id,user_id,car_id,place_id,time,status) values(0, '%1', '%2', '%3','%4',0)").arg(userid1).arg(carid1).arg(placeid).arg(time);
                    QString str4 = QString("insert into book1(id,username,carname,time,place_id) values(0, '%1', '%2', '%3','%4')").arg(user).arg(carid).arg(time).arg(placeid);
                    if(query.exec(str3)){
                        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("预约成功"), QMessageBox::Yes);
                        query.exec(str4);
                        QString str5=QString("update place set status=2 where id='%1';").arg(place_id);
                        query.exec(str5);
                        QDateTime time = QDateTime::currentDateTime();
                        QString str = time.toString("yyyy-MM-dd hh:mm:ss");
                        ui->label_25->setText(place_id);
                        ui->label_26->setText(str);
                        //刷新页面
                        emit(send_user_flush());
                    }
                    else
                    {
                        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("预约失败"), QMessageBox::Yes);
                    }
                }
            }
        }
    }

}
void userwindow::reshow(){

    this->show();
}

//预订记录
void userwindow::on_pushButton_7_clicked()
{
    bk.setuser(this->user);
    bk.show();
}
void userwindow::sqlinfo()
{
    bk.printinfo(user);
}
//刷新界面
void userwindow::on_pushButton_12_clicked()
{
    emit(send_user_flush());
    QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("刷新成功"), QMessageBox::Yes);
}
//停车
void userwindow::on_pushButton_2_clicked(){
    //用户id
    QString id=this->user_id;
    query.exec(QString("select * from book where user_id='%1' and status=0").arg(id));
    QString car_id,place_id;
    if (query.next()) {
        car_id=query.value("car_id").toString();
        place_id=query.value("place_id").toString();
    }


    if(place_id.isEmpty())
      {
       QMessageBox::warning(nullptr, QStringLiteral("停车失败"), QStringLiteral("请重新预约"), QMessageBox::Yes);
    }
    else{
        int placeid=place_id.toInt();
        stopnav.setid(placeid);//把当前id传到stopnav
        QDateTime time = QDateTime::currentDateTime();
        QString str = time.toString("yyyy-MM-dd hh:mm:ss");
        stopnav.show();
        //把记录插入停车表
        query.exec(QString("insert into stopinfo(id,user_id,car_id,place_id,intime,outtime) values(0,'%1','%2','%3','%4',null)").arg(id).arg(car_id).arg(place_id).arg(str));
        //query.exec(QString("delete from book where place_id='%1'").arg(place_id));
        query.exec(QString("update book set status=1 where user_id='%1' and car_id='%2';").arg(id).arg(car_id));
        //刷新页面
        emit(send_user_flush());
       }
}
//出车
void userwindow::on_pushButton_3_clicked()
{
    QString carid=ui->comboBox_6->currentText(),car_id,place_id;
    QString str1 = QString("select id from carinfo where carid='%1';").arg(carid);

    query.exec(str1);
    if(query.first()){
        car_id=query.value(0).toString();
    }
    QString curtime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString str2=QString("update stopinfo set outtime='%1' where user_id='%2' and car_id='%3';").arg(curtime).arg(user_id).arg(car_id);
    QString str3= QString("select place_id from stopinfo where user_id='%1' and car_id='%2' and outtime is null;").arg(user_id).arg(car_id);
    query.exec(str3);
    if(query.first()){
        place_id=query.value(0).toString();
    }
    qDebug()<<"stopcar"<<place_id;
    QString str4=QString("update place set status=0 where id='%1';").arg(place_id);

    if(query.exec(str2) &&query.exec(str4)){
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("停车成功"), QMessageBox::Yes);
        //刷新页面
        emit(send_user_flush());
        //结算ui弹出

        //结算.show()
        //结算设置 carid place_id
    }
    else{
        QMessageBox::warning(nullptr, QStringLiteral("失败"), QStringLiteral("出车失败"), QMessageBox::Yes);
    }
}

//充值金额
void userwindow::emit_user_balance(){
    QString tmp = ui->lineEdit_11->text();
    if(tmp.isEmpty()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("请输入您需要充值的金额"), QMessageBox::Yes);
    }
    else{
        ui->lineEdit_11->clear();
        bool ok;
        int fare = tmp.toInt(&ok, 10);
        emit(send_user_balance(fare));
        QSqlQuery query;
        //qDebug()<<user_id;
        QString inquire = QString("select fare from userinfo where username = '%1'").arg(user);
        //qDebug()<<user_id;
        query.exec(inquire);
        qDebug()<<fare;
        while(query.next()){
            fare += query.value(0).toInt();
        }
        QString str = QString("update userinfo set fare = '%1' where username = '%2'").arg(fare).arg(user);
        query.exec(str);
        QString user_id;
        int userid1;
        while(query.next())
        {
            user_id = query.value(0).toString();
            userid1= user_id.toInt();
        }
        QString str_2 = QString("update consuption set fare = '%1' where user_id = '%2'").arg(fare).arg(userid1);
        query.exec(str_2);
//        QString str1 = QString("select id from userinfo where username = '%1'").arg(user);
//        query.exec(str1);


        if(query.exec(str)){
            QMessageBox::warning(nullptr, QStringLiteral("提示"), QStringLiteral("充值成功"), QMessageBox::Yes);

            }
            else
            {
                QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("充值失败"), QMessageBox::Yes);
            }

    }

}

//显示结算的相关信息
void userwindow::consuinfo(){

    QString user_id,place_id,in_time,out_time;
    int userid1;
    QSqlQuery query;

    QString str1 = QString("select id from userinfo where username = '%1'").arg(user);
    query.exec(str1);
    while(query.next())
    {
        user_id = query.value(0).toString();
        userid1= user_id.toInt();
    }

    QString str2 = QString("select place_id,intime,outtime from stopinfo where user_id = '%1'").arg(userid1);
    query.exec(str2);
    while(query.next())
    {
        place_id = query.value(0).toString();
        in_time=query.value(1).toString();
        out_time=query.value(2).toString();

    }
    ui->textEdit->clear();
    QString str3="id  regular_time  per_time_money\n\n";
    ui->textEdit->append(str3);
    QString str4="regular_time  per_time_money\n\n";
    ui->textEdit_2->append(str4);
    query.exec("select * from regular");
    while(query.next())
    {
        QString s=query.value(0).toString()+"      "+query.value(2).toString()+"      "+query.value(3).toString()+"\n\n";
        ui->textEdit->append(s);
        QString s1=query.value(2).toString()+"      "+query.value(3).toString()+"\n";
        ui->textEdit_2->append(s1);
    }
    ui->lineEdit_8->setText(place_id);//车位
    ui->lineEdit_9->setText(in_time);//开始时间
    ui->lineEdit_10->setText(out_time);//结束时间
    //ui->textEdit->append("123");
    //金额
    QString str = QString("select flag from userinfo where username = '%1'").arg(user);
    int amount=0;
    query.exec(str);
    while(query.next())
    {
        flag = query.value(0).toString();
        //userid1= user_id.toInt();
    }
    //qDebug()<<flag;
    if(flag=="年费用户"||flag=="月费用户"||"季费用户")
    {
        amount=0;
    }
    if(flag=="普通用户"){
        QString str5 = QString("select per_time_money from regular where regular_type = 1");
        query.exec(str5);
        int money = 0;
        while(query.next()){
            money = query.value(0).toInt();
        }
        //qDebug()<<money;
        QString str9 = QString("select intime,outtime from stopinfo WHERE user_id=7");
        query.exec(str9);
        QString i,o;
        while(query.next()){
            i = query.value(0).toString();\
            o = query.value(1).toString();
        }
        //qDebug()<<i;
        //qDebug()<<o;
        QString str10 = QString("select TIMESTAMPDIFF(HOUR,'%1','%2')").arg(i).arg(o);
        query.exec(str10);
        int h;
        while(query.next()){
            h = query.value(0).toInt();\
        }
        //qDebug()<<h;
        //qDebug()<<money;
        amount=money*h;
    }
    QString str6 = QString::number(amount);
    ui->lineEdit_7->setText(str6);//结算费用
    QString fare;
    QString str7 = QString("select fare from userinfo where user_id = '%1'").arg(userid1);
    query.exec(str7);
    while(query.next())
    {
        fare = query.value(0).toString();
    }
    query.exec(str7);
    QString str8 = QString("insert into consuption(id,place_id,user_id,fare,amount) values(0,'%1', '%2', '%3', '%4');").arg(place_id).arg(userid1).arg(fare).arg(amount);//0代表id自增
    //QString str7 = QString("ins consuption set amount = '%1' where user_id = '%2'").arg(amount).arg(user_id);
    query.exec(str8);
}

//结算操作
void userwindow::consuption(){
    QString user_id;
    int userid1;
    QSqlQuery query;
    QString str1 = QString("select id from userinfo where username = '%1'").arg(user);
    query.exec(str1);
    while(query.next())
    {
        user_id = query.value(0).toString();
        userid1= user_id.toInt();
    }
    QString str2 = QString("select fare from consuption where user_id = '%1'").arg(userid1);
    query.exec(str2);

    int fare = 0;
    while(query.next()){
        fare = query.value(0).toInt();
    }
    //qDebug()<<fare;
    QString str3 = QString("select amount from userinfo where username = '%1'").arg(userid1);
    query.exec(str3);
    int amount = 0;
    while(query.next()){
        amount = query.value(0).toInt();
    }
    //qDebug()<<amount;
    if(fare<amount){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("余额不足，请充值"), QMessageBox::Yes);
    }
    else{
        fare-=amount;
        QString str_4 = QString("update userinfo set fare = '%1' where username = '%2'").arg(fare).arg(user);
        QString str_5 = QString("update consuption set fare = '%1' where username = '%2'").arg(fare).arg(user);
        query.exec(str_4);
        query.exec(str_5);
        QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("结算成功"), QMessageBox::Yes);
        this->sqlinfo();
    }
}

void userwindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
     if(ui->comboBox->currentIndex() == 0)
     {
         qDebug()<<"优秀";
     }
     else if(ui->comboBox->currentIndex()==1)
     {
         qDebug()<<"good";
     }

 }

void userwindow::SetFlag(QString)
{
    QSqlQuery query;
    QString inquire = QString("select fare from userinfo where username = '%1'").arg(user);
    query.exec(inquire);
    int fare = 0;
    while(query.next()){
        fare = query.value(0).toInt();
    }
    if(ui->comboBox->currentText()== "年费用户")
    {
        //qDebug()<<"111";
        QString str = QString("select per_time_money from regular where regular_type = 4");
        query.exec(str);
        int money = 0;
        while(query.next()){
            money = query.value(0).toInt();
        }
        qDebug()<<money;
        if(fare < money){
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("余额不足，请充值"), QMessageBox::Yes);
        }
        else{
            fare -= money;
            QString str = QString("update userinfo set fare = '%1' where username = '%2'").arg(fare).arg(user);
            QString str_2 = QString("update userinfo set flag = '年费用户' where username = '%1'").arg(user);
            QString str_3 = QString("update consuption set fare = '%1' where username = '%2'").arg(fare).arg(user);
            query.exec(str);
            query.exec(str_2);
            query.exec(str_3);
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("会员办理成功"), QMessageBox::Yes);
            this->sqlinfo();
        }

    }
    else if(ui->comboBox->currentText()=="季费用户")
    {
        qDebug()<<"222";
        QString str2 = QString("select per_time_money from regular where regular_type = 3");
        query.exec(str2);
        int money = 0;
        while(query.next()){
            money = query.value(0).toInt();
        }
        qDebug()<<money;
        QString inquire_2 = QString("select flag from userinfo where username = '%1'").arg(user);
        query.exec(inquire_2);
        QString type;
        while(query.next()){
            type = query.value(0).toString();
        }
        if(type == "年费用户"){
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("您已是年费用户"), QMessageBox::Yes);
        }
        else{
            if(fare < money){
                QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("余额不足，请充值"), QMessageBox::Yes);
            }
            else{
                fare -= money;
                QString str = QString("update userinfo set fare = '%1' where username = '%2'").arg(fare).arg(user);
                QString str_2 = QString("update userinfo set flag = '季费用户' where username = '%1'").arg(user);
                QString str_3 = QString("update consuption set fare = '%1' where username = '%2'").arg(fare).arg(user);
                query.exec(str_3);
                query.exec(str);
                query.exec(str_2);
                QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("会员办理成功"), QMessageBox::Yes);
                this->sqlinfo();
            }
        }
    }
    else if(ui->comboBox->currentText()=="月费用户")
    {
        qDebug()<<"333";
        QString str2 = QString("select per_time_money from regular where regular_type = 2");
        query.exec(str2);
        int money = 0;
        while(query.next()){
            money = query.value(0).toInt();
        }
        qDebug()<<money;
        QString inquire_2 = QString("select flag from userinfo where username = '%1'").arg(user);
        query.exec(inquire_2);
        QString type;
        while(query.next()){
            type = query.value(0).toString();
        }
        if(type == "年费用户"){
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("您已是年费用户"), QMessageBox::Yes);
        }
        if(type == "季费用户"){
            QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("您已是季费用户"), QMessageBox::Yes);
        }
        else{
            if(fare < money){
                QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("余额不足，请充值"), QMessageBox::Yes);
            }
            else{
                fare -= money;
                QString str = QString("update userinfo set fare = '%1' where username = '%2'").arg(fare).arg(user);
                QString str_2 = QString("update userinfo set flag = '月费用户' where username = '%1'").arg(user);
                QString str_3 = QString("update consuption set fare = '%1' where username = '%2'").arg(fare).arg(user);
                query.exec(str_3);
                query.exec(str);
                query.exec(str_2);
                QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("会员办理成功"), QMessageBox::Yes);
                this->sqlinfo();
            }
        }
    }

}
